package vcp.client.GUI.mvc.model;

import java.util.ArrayList;

import vcp.client.forms.SiteCostUpdateFrom;
import vcp.client.interfaces.IObservable;
import vcp.client.request.SiteCostUpdateRequest;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Site;
import vcp.entity.SiteCostUpdates;
import vcp.entity.Worker;
import vcp.enums.SiteEnum.SiteCostsUpdateStatus;

/**
 * 
 * @author VCP team
 *         <p>
 *         coast update model
 *         </p>
 */
public class UIUpdateCostsModel extends UIAbstractModel
{
	/** instance of worker **/
	private Worker siteManager;

	public Worker getSiteManager ()
	{
		return this.siteManager;
	}

	public void setSiteManager ( Worker siteManager )
	{
		this.siteManager = siteManager;
	}

	/**
	 * update the observable model
	 * 
	 * @param model
	 */
	public UIUpdateCostsModel ( IObservable model )
	{
		super(model);
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * 
	 * @return site name of the worker
	 */
	public Site getWorkerSite ()
	{
		return this.siteManager.getSites().get(0);
	}

	/**
	 * 
	 * @return the current site coast
	 */
	public SiteCostUpdates getWorkerSiteUpdate ()
	{
		return this.siteManager.getSiteCostUpdates().isEmpty()
																? null
																: this.siteManager.getSiteCostUpdates().get(0);
	}

	/**
	 * Request for update the site coast to new
	 * 
	 * @param form
	 */
	public void applySiteCostUpdate ( SiteCostUpdateFrom form )
	{
		SiteCostUpdateRequest request = new SiteCostUpdateRequest(form);
		this.sendRequest(request, true);
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * send answer for update request
	 * 
	 * @param siteCostUpdate
	 */
	private void updateWorkerSiteCostsByAnsweredRequest ( SiteCostUpdates siteCostUpdate )
	{
		Site workerSite = this.getWorkerSite();

		workerSite.setSingleUsePricePerHour(siteCostUpdate.getSingleUsePricePerHour());
		workerSite.setOrderAheadPricePerHour(siteCostUpdate.getOrderAheadPricePerHour());
		workerSite.setCasualPriceSingleCar(siteCostUpdate.getCasualPriceSingleCar());
		workerSite.setFullPriceSingleCar(siteCostUpdate.getFullPriceSingleCar());
	}

	/**
	 * update the site coast to new
	 * 
	 * @param siteCostUpdate
	 */
	private void updateSiteCostUpdates ( SiteCostUpdates siteCostUpdate )
	{
		this.siteManager.getSiteCostUpdates().add(siteCostUpdate);
	}

	/**
	 * get the site to update his coasts
	 * 
	 * @param updateCosts
	 * @return relevant site cost update
	 */
	private SiteCostUpdates getRelevantSiteCostUpdate ( ArrayList <SiteCostUpdates> updateCosts )
	{
		SiteCostUpdates foundSiteCostUpdates = null;
		for ( SiteCostUpdates siteCostUpdates : updateCosts )
		{
			if ( siteCostUpdates.getSiteEnum() == this.getWorkerSiteUpdate().getSiteEnum() )
			{
				foundSiteCostUpdates = siteCostUpdates;
				this.siteManager.getSiteCostUpdates().remove(0);
				break;
			}
		}
		return foundSiteCostUpdates;
	}

	/****************************************************************************************************************************
	 *** UIAbstractModel methods ***
	 ****************************************************************************************************************************/
	@Override
	public void clearModel ()
	{
		this.siteManager = null;
	}

	@Override
	public void shouldRegisterForUpdates ()
	{
		this.model.registerForUpdates(this);
	}

	/****************************************************************************************************************************
	 *** Observer methods ***
	 ****************************************************************************************************************************/
	@Override
	public void update ( Object data )
	{
		if ( data instanceof SiteCostUpdates )
		{
			SiteCostUpdates updateCosts = (SiteCostUpdates) data;
			this.updateSiteCostUpdates(updateCosts);

			if ( ! this.siteManager.getSiteCostUpdates().isEmpty() )
			{
				if ( this.getWorkerSiteUpdate().getStatus() != SiteCostsUpdateStatus.PENDING )
				{
					this.siteManager.getSiteCostUpdates().remove(0);
				}
			}

			this.controllerDelegate.updateControllerDelegateWithData(null);
			this.controllerDelegate.loadingStop();
		}
	}

	@SuppressWarnings ( "unchecked" )
	@Override
	public void updateWithReferesh ( Object data )
	{
		if ( data instanceof ArrayList <?> )
		{
			ArrayList <SiteCostUpdates> updateCosts = (ArrayList <SiteCostUpdates>) data;

			SiteCostUpdates relevantSiteCostUpdate = this.getRelevantSiteCostUpdate(updateCosts);
			this.updateSiteCostUpdates(relevantSiteCostUpdate);
			this.updateWorkerSiteCostsByAnsweredRequest(relevantSiteCostUpdate);

			this.controllerDelegate.updateControllerDelegateWithData(String.format(UserNotification.__VALUE__IS_UPDATED, relevantSiteCostUpdate.getSiteName()));
			this.controllerDelegate.updateControllerDelegateWithData(null);
			this.controllerDelegate.loadingStop();
		}
	}

}
